home *** CD-ROM | disk | FTP | other *** search
- #ifdef __TURBOC__
- #include <conio.h>
- #else
- #include <graph.h>
- #endif
- #include <dos.h>
- #include <stdio.h>
- #include <string.h>
- #include "db_types.h"
- #include "db_lsc.h"
- #include "db_conio.h"
- #include "db_curs.h"
- #include "db_date.h"
- #include "db_dos.h"
- #include "db_file.h"
- #include "db_funcs.h"
- #include "db_gvar.h"
- #include "db_heap.h"
- #include "db_key.h"
- #include "db_man.h"
- #include "db_sets.h"
- #include "db_str.h"
- #include "db_tree.h"
- #include "db_win.h"
- #include "db_mnu.h"
- #include "db_util.h"
-
- unsigned _stklen = 0x4000; /* Default stack of 16 K */
-
- #define TempFName "_UPDTEMP.DAT"
- #define UpdExt ".UPD"
- #define MemoRecStat -2
-
- typedef uchar genbuf[DB_mxdrs];
- typedef enum {_Byte,_StrPtr} Varieties;
-
- typedef struct {
- byte v; /* type changed from Varieties so storage is PASCAL compatible */
- byte b;
- strptr s;
- } multityp;
-
- typedef struct {
- uchar typ;
- byte len;
- byte dec;
- strptr strp;
- } fielddescr;
-
- typedef fielddescr manyfields[DB_mxdrs];
- typedef manyfields *fldsptr;
- typedef multityp updtabtyp[DB_mxdrs];
- typedef updtabtyp *updtabptr;
-
-
- bool quit;
- uchar ctlkey;
- mnufrec dm;
- int oldsz,newsz;
- int numold,numnew;
- pathstr infoname,ufname1,ufname2;
- fldsptr oldflds,newflds;
- updtabptr updtab;
- datafile odatf,ndatf;
- genbuf trec1,trec2;
- genbuf oldrec,updrec;
- long *oldrecstat = (long *) oldrec;
- long *updrecstat = (long *) updrec;
-
- void clearbuf(genbuf buf, int numf, fldsptr flds)
- {
- int i;
- word fofs;
- string ts;
-
- memset(buf,' ',DB_mxdrs);
- for (i=0; i<4; i++) buf[i] = 0;
- fofs = 4;
- for (i=0; i<numf; i++) {
- switch ((*flds)[i].typ) {
- case _Ch :
- buf[fofs + (*flds)[i].len] = Nul;
- break;
- case _Num :
- fstr(ts,0.0,(*flds)[i].len,(*flds)[i].dec);
- memmove(&buf[fofs],ts,(*flds)[i].len+1);
- break;
- case _Memo : memset(&buf[fofs],0,4); break;
- }
- fofs += (*flds)[i].len;
- if ((*flds)[i].typ != _Memo) fofs++;
- }
- }
-
- strptr getsptr(int ind, fldsptr flds, ptr buf)
- {
- int i;
- word fofs;
-
- fofs = 4;
- for (i=0; i<ind; i++) {
- fofs += (*flds)[i].len;
- if ((*flds)[i].typ != _Memo)
- fofs++;
- }
- return(MK_FP(FP_SEG(buf),FP_OFF(buf)+fofs));
- }
-
- bool chkexists(strptr instr)
- {
- string ts;
-
- if (!exists(instr) || (exitcode == F2)) {
- if ((strchr(instr,'*') != NULL) || (strchr(instr,'?') != NULL))
- dirpick(instr,strip(ts,instr),DirIncl,DirExcl | A_RDONLY,0,0);
- else
- dirpick(instr,"*.DAT",DirIncl,DirExcl | A_RDONLY,0,0);
- exitcode = ' ';
- }
- return((bool)(exists(instr) || empty(instr)));
- }
-
- bool chknew(strptr instr)
- {
- uchar ans[2],no[2],tpic[2];
-
- chstr(ans,_YES);
- chstr(no,_NO);
- chstr(tpic,Pic_U);
- if (exists(instr)) dbgetstr(ans,_Ch,LSC_OverwriteExisting,no,tpic,"",chkyesno,nohelp);
- return((bool)(ans[0] == _YES));
- }
-
- bool selectnames(void)
- {
- int doublepos;
- string ts;
- uchar tpic[4];
-
- if (strchr(ufname1,'\\') == NULL) strconcat(ufname1,datapath,"\\",ufname1,NULL);
- doublepos = strposstr("\\\\",ufname1);
- if (doublepos >= 0) strdelete(ufname1,doublepos,1);
- strcpy(tpic,"64");
- strchcat(tpic,Pic_U);
- dbgetstr(ts,_Ch,LSC_UpdateIn,ufname1,tpic,"",chkexists,nohelp);
- strip(ufname1,ts);
- if (exitcode != QitKey) {
- strcpy(ufname2,ufname1);
- dbgetstr(ts,_Ch,LSC_CreatingUpdFile,ufname2,tpic,"",chknew,nohelp);
- strip(ufname2,ts);
- }
- return((bool)(exitcode != QitKey));
- }
-
-
- bool loadinfo(void)
- {
- bool fval;
- byte b;
- int i;
- FILE *infofile;
-
- infofile = fopen(infoname,"rb");
- if (infofile != NULL) {
- fread(&b,1,1,infofile);
- ufname1[b] = '\0';
- fread(ufname1,1,b,infofile);
- fread(&oldsz,sizeof(oldsz),1,infofile);
- fread(&numold,sizeof(numold),1,infofile);
- oldflds = db_malloc(numold*sizeof(fielddescr));
- fread(oldflds,sizeof(fielddescr),numold,infofile);
- for (i=0; i<numold; i++) (*oldflds)[i].strp = getsptr(i,oldflds,oldrec);
- fread(&b,1,1,infofile);
- ufname2[b] = '\0';
- fread(ufname2,1,b,infofile);
- fread(&newsz,sizeof(newsz),1,infofile);
- fread(&numnew,sizeof(numnew),1,infofile);
- newflds = db_malloc(numnew*sizeof(fielddescr));
- fread(newflds,sizeof(fielddescr),numnew,infofile);
- for (i=0; i<numnew; i++) (*newflds)[i].strp = getsptr(i,newflds,updrec);
- updtab = db_malloc(numnew*sizeof(multityp));
- for (i=0; i<numnew; i++) {
- fread(&((*updtab)[i].v),sizeof((*updtab)[i].v),1,infofile);
- switch ((Varieties) (*updtab)[i].v) {
- case _Byte :
- fread(&((*updtab)[i].b),1,1,infofile);
- if ((*updtab)[i].b > 0)
- (*updtab)[i].s = (*oldflds)[(*updtab)[i].b-1].strp;
- else
- (*updtab)[i].s = NULL;
- break;
- case _StrPtr :
- fread(&b,1,1,infofile);
- (*updtab)[i].s = db_malloc(b+1);
- (*updtab)[i].s[b] = '\0';
- fread((*updtab)[i].s,1,b,infofile);
- (*updtab)[i].b = 0;
- break;
- }
- }
- fclose(infofile);
- fval = selectnames();
- }
- else fval = False;
- return(fval);
- }
-
- void newmemos(ptr oldm, ptr newm)
- {
- long *oldmemoref,*newmemoref;
- long tl,tr1,tr2,totb,savm;
- long *memostat,*memolink;
-
- oldmemoref = (long *) oldm;
- newmemoref = (long *) newm;
- totb = 0L;
- savm = *oldmemoref;
- *newmemoref = 0L;
- if (savm > 0L) {
- while (savm != 0L) {
- getrec(&odatf,savm,&trec1[(int) totb]);
- memmove(&savm,&trec1[(int) (totb+oldsz-4)],4);
- memmove(&trec1[(int) totb],&trec1[(int) (totb+4)],oldsz-8);
- totb += oldsz-8;
- }
- tl = 0L;
- memostat = (long *) trec2;
- memolink = (long *) &trec2[newsz-4];
- addrec(&ndatf,&tr1,trec2);
- *newmemoref = tr1;
- while (totb > 0L) {
- memset(trec2,' ',newsz);
- *memostat = MemoRecStat;
- if (totb >= (newsz-8))
- memmove(&trec2[4],&trec1[(int) tl],newsz-8);
- else
- memmove(&trec2[4],&trec1[(int) tl],(int) (totb+1));
- totb -= newsz-8;
- tl += newsz-8;
- if (totb > 0) {
- addrec(&ndatf,&tr2,trec2);
- *memolink = tr2;
- }
- else *memolink = 0L;
- putrec(&ndatf,tr1,trec2);
- tr1 = tr2;
- }
- }
- }
-
- void updaterec(long _auto)
- {
- int i;
- string ts, cmp;
-
- for (i=0; i<numnew; i++) if ((*updtab)[i].s != NULL) {
- if ((*newflds)[i].typ != _Memo) {
- strcpy(ts,(*updtab)[i].s);
- upper(cmp,ts);
- if (strcmp(cmp,"SYSDATE") == 0) sysdate(ts);
- else if (strcmp(cmp,"AUTO") == 0) strip(ts,istr(ts,_auto,10));
- switch ((*newflds)[i].typ) {
- case _Ch :
- strcopy(ts,ts,0,(*newflds)[i].len);
- pad((*newflds)[i].strp,ts,(*newflds)[i].len,Right);
- break;
- case _Num :
- fstr((*newflds)[i].strp,valu(ts),(*newflds)[i].len,(*newflds)[i].dec);
- break;
- }
- }
- else newmemos((*updtab)[i].s,(*newflds)[i].strp);
- }
- }
-
- void updatedata(void)
- {
- long li,_auto,trec,recs;
- winptr twinp;
- string ts;
-
- if (openwin(0,1,1,scrwid,scrhgt,swc,sbc,0,"─┐│┘─└│┌",TopCnt,"")) {
- twinp = curwin;
- hidewin(T_ON,twinp);
- }
- else twinp = NULL;
- openfile(&odatf,ufname1,oldsz);
- if (ok) {
- makefile(&ndatf,TempFName,newsz);
- if (ok) {
- writewxy(centre(ts,LSC_UpdatingData,80),0,1,1,twinp);
- writewxy(centre(ts,ufname1,80),0,1,2,twinp);
- writewxy(centre(ts,LSC_ToFile,80),0,1,3,twinp);
- writewxy(centre(ts,ufname2,80),0,1,4,twinp);
- strconcat(ts,LSC_Recs2Process,istr(ts,usedrecs(&odatf),10),NULL);
- writewxy(ts,0,10,6,twinp);
- writewxy(LSC_RecsProcessed,0,10,7,twinp);
- _auto = 0L;
- clearbuf(updrec,numnew,newflds);
- recs = filelen(&odatf)-1;
- for (li=1L; li<=recs; li++) {
- writewxy(istr(ts,li,10),0,31,7,twinp);
- getrec(&odatf,li,oldrec);
- if (*oldrecstat == 0L) {
- updaterec(++_auto);
- addrec(&ndatf,&trec,updrec);
- }
- }
- closefile(&odatf);
- closefile(&ndatf);
- if (exists(ufname2))
- unlink(ufname2);
- rename(TempFName,ufname2);
- writewxy(centre(ts,LSC_PressAnyKey,80),0,1,scrhgt,twinp);
- getkey();
- }
- else {
- closefile(&odatf);
- dspmsge("",LSC_BadTempFile,0.0);
- }
- }
- else
- dspmsge("",LSC_BadOrigFile,0.0);
- if (twinp != NULL)
- closewin(&twinp);
- }
-
- void do_proc(int procno, mnufrec *m)
- {
- string ts,spec;
-
- switch (procno) {
- case 1 :
- strcat(chstr(spec,'*'),UpdExt);
- dirpick(ts,spec,DirIncl,DirExcl | A_RDONLY,0,0);
- strip(infoname,ts);
- break;
- case 2 :
- if (infoname[0] != '\0') {
- if (loadinfo()) updatedata();
- }
- else
- dberrm(LSC_SelectUPDFirst);
- break;
- case 3 :
- quit = True;
- break;
- }
- }
-
- void do_ctl(mnufrec *m, itmptr itm)
- {
- if ((m->curmnu != NULL) && (itm != NULL)) {
- switch (itm->seltyp) {
- case _Jump :
- jumptomnu(m,(byte)ival(itm->selact));
- break;
- case _Menu :
- if ((ival(itm->selact) > 0) &&
- (m->mat[(int) ival(itm->selact)-1] != NULL)) {
- if (m->mat[(int) ival(itm->selact)-1]->winp != NULL)
- jumptomnu(m,(byte)ival(itm->selact));
- else {
- m->mat[(int) ival(itm->selact)-1]->pmnu = m->curmnu->mno;
- m->curmnu = m->mat[(int) ival(itm->selact)-1];
- dspmnu(m,m->curmnu);
- m->curitm = m->curmnu->litm;
- m->level = 0;
- }
- }
- break;
- case _Proc :
- do_proc((int) ival(itm->selact),m);
- break;
- case _Quit :
- quit = True;
- break;
- }
- }
- }
-
- void loadmanual(void)
- {
- string tfnam;
- fspec fr;
-
- fsplit(strip(tfnam,dm.mnufnam),fr.fdir,fr.fname,fr.fext);
- strconcat(fr.fpath,fr.fdir,DatabossManual,NULL);
- if (!exists(fr.fpath)) strconcat(fr.fpath,fr.fdir,fr.fname,".MAN",NULL);
- initmanual(fr.fpath);
- }
-
- bool initdbm(void)
- {
- strcpy(dm.mnufnam,"DBUPD.DMU");
- initmnu(&dm,0,0,OnDisk);
- return((bool)(dm.curmnu != NULL));
- }
-
- void main(void)
- {
- string ts;
- uchar yes[2],tpic[2];
-
- db_curs_init();
- db_date_init();
- db_funcs_init();
- db_gvar_init();
- db_heap_init();
- db_key_init();
- db_man_init();
- db_tree_init();
- db_win_init();
- db_mnu_init();
- db_util_init();
-
- chstr(yes,_YES);
- chstr(tpic,Pic_U);
- initedit();
- initindex();
- set_addmember(ExitSet,AltX);
- if (initdbm()) {
- loadmanual();
- clrscr();
- dispallwin();
- quit = False;
- dspmnu(&dm,dm.curmnu);
- infoname[0] = '\0';
- do {
- hbar(&dm);
- ctlkey = upperch(getkey());
- if ((ctlkey == DArr) && (!mousekey) &&
- ((dm.curmnu->mtyp != Vert) && (dm.curitm->seltyp == _Menu)))
- ctlkey = Enter;
- if (set_member(dm.mnualtset,ctlkey))
- processalt(ctlkey,&dm,do_ctl);
- else {
- switch (ctlkey) {
- case QitKey :
- if (backjump > 0) {
- jumptomnu(&dm,backjump);
- backjump = 0;
- }
- else {
- if ((dm.curmnu != NULL) && (dm.curmnu->pmnu != 0)) {
- zapmnu(&dm,dm.curmnu);
- dm.curmnu->litm = dm.curitm;
- dm.curmnu = dm.mat[dm.curmnu->pmnu-1];
- dm.curitm = dm.curmnu->litm;
- dm.level = 0;
- if (dm.curmnu->winp == NULL)
- dspmnu(&dm,dm.curmnu);
- }
- else
- quit = True;
- }
- break;
- case UArr : goud(&dm,Up); break;
- case DArr : goud(&dm,Down); break;
- case LArr : gorl(&dm,Left); break;
- case RArr : gorl(&dm,Right); break;
- case Enter:
- if (gvar->sec >= dm.curitm->sec) {
- if ((backjump > 0) && (dm.curitm->seltyp != _Proc))
- backjump = 0;
- do_ctl(&dm,dm.curitm);
- if (backjump > 0) {
- jumptomnu(&dm,backjump);
- backjump = 0;
- }
- }
- break;
- case HlpKey : itemhelp(&dm); break;
- case Home : firstitm(&dm); break;
- case EndKey : lastitm(&dm); break;
- case AltV : setdeltas(); break;
- case AltX : quit = True; break;
- default :
- if (selitm(ctlkey,&dm)) {
- hbar(&dm);
- if ((backjump > 0) && (dm.curitm->seltyp != _Proc))
- backjump = 0;
- do_ctl(&dm,dm.curitm);
- if (backjump > 0) {
- jumptomnu(&dm,backjump);
- backjump = 0;
- }
- }
- break;
- }
- }
- if (quit) {
- dbgetstr(ts,_Ch,LSC_QuitProgram,yes,tpic,"",chkyesno,nohelp);
- quit = (bool)((ts[0] == _YES) && (exitcode != QitKey));
- }
- } while (!quit);
- disposemnus(&dm);
- }
- else {
- writewxy(centre(ts,LSC_BadMenu,80),15,1,25,NULL);
- delayorkey(2.0);
- }
- clrscr();
- gotoxy(1,25);
- }
-